From: Florian Müllner Date: Mon, 10 Jul 2023 03:53:45 +0000 (+0200) Subject: extensions: Make ExtensionPreferences more flexible X-Git-Tag: archive/raspbian/50.5-1+rpi1~1^2~337^2~1^2~90 X-Git-Url: https://dgit.raspbian.org/%22http://www.example.com/cgi/%22/%22http:/www.example.com/cgi/%22?a=commitdiff_plain;h=797df4f52f569eb76e469502e76516051703db80;p=gnome-shell.git extensions: Make ExtensionPreferences more flexible Extensions must now export a class with a fillPreferencesWindow() method in their prefs. That is less convenient for extensions with simple preferences than the old buildPrefsWidget() hook, as they must wrap their widget in page/group widgets. Address this by adding a default fillPreferencesWindow() implementation that calls a getPreferencesWidget() method and wraps it as necessary. This is flexible enough to support different cases fairly conveniently, from simple single-widget prefs over tweaking the window to complex multi-page prefs: ```js class SimplePreferences extends ExtensionPreferences { getPreferencesWidget() { return new SimplePrefsWidget(); } } class TinkerPreferences extends ExtensionPreferences { getPreferencesWidget() { return new SimplePrefsWidget(); } fillPreferencesWindow(window) { super.fillPreferencesWindow(window); window.set_default_size(123, 456); } } class FullPreferences extends ExtensionPreferences { fillPreferencesWindow(window) { const page1 = new GeneralPage(); window.add(page1); const page2 = new FoobarPage(); window.add(page2); } } ``` Part-of: --- diff --git a/js/extensions/prefs.js b/js/extensions/prefs.js index d60859a3d..f36b97138 100644 --- a/js/extensions/prefs.js +++ b/js/extensions/prefs.js @@ -1,3 +1,4 @@ +import Adw from 'gi://Adw'; import GObject from 'gi://GObject'; import {ExtensionBase, GettextWrapper} from './sharedInternals.js'; @@ -14,13 +15,45 @@ export class ExtensionPreferences extends ExtensionBase { } /** - * Fill the preferences window with preferences. + * Get the single widget that implements + * the extension's preferences. * - * @param {Adw.PreferencesWindow} _window - the preferences window + * @returns {Gtk.Widget} */ - fillPreferencesWindow(_window) { + getPreferencesWidget() { throw new GObject.NotImplementedError(); } + + /** + * Fill the preferences window with preferences. + * + * The default implementation adds the widget + * returned by getPreferencesWidget(). + * + * @param {Adw.PreferencesWindow} window - the preferences window + */ + fillPreferencesWindow(window) { + const widget = this.getPreferencesWidget(); + const page = this._wrapWidget(widget); + window.add(page); + } + + _wrapWidget(widget) { + if (widget instanceof Adw.PreferencesPage) + return widget; + + const page = new Adw.PreferencesPage(); + if (widget instanceof Adw.PreferencesGroup) { + page.add(widget); + return page; + } + + const group = new Adw.PreferencesGroup(); + group.add(widget); + page.add(group); + + return page; + } } export const {